//
//  ZYFinancialProductCell.swift
//  HengYouCai
//
//  Created by lwj on 2018/6/25.
//  Copyright © 2018年 lwj. All rights reserved.
//

import UIKit

class ZYFinancialProductCell: UITableViewCell {
 //首页使用的 方法
    override func wl_configurationCell(_ modelTemp: Any?, _ listView: UIScrollView, _ indexPath: IndexPath) {
        if let dataArr = modelTemp as? [ProjectModel] {
            let model = dataArr[indexPath.row]
            
            // title
            self.titleLab.text =  model.title
            
            // 标签
            self.tagView.tagsStr(model.tags)
            
            if model.cashRateShow == "1" {
                self.minProfitLab.attributedText = handlerProfit(model.profit,model.cashRateProfit)
            }else{
                self.minProfitLab.attributedText = handlerProfit(model.profit,model.profitFloat)
            }
            
            // 期限 处理富文本编辑框
            let text = NSMutableAttributedString(string: model.plstimeLimitValue, attributes: [NSForegroundColorAttributeName:ColorFromRGBHex_WL(0x5B9CF8),NSFontAttributeName:Font_WL(20.aw_wl)])
            
            text.append(NSAttributedString(string: model.prdtimeLimitTypeLabel, attributes: [NSForegroundColorAttributeName : Normal_999Color,NSFontAttributeName:Font_WL(11.aw_wl)]))
            
            self.plstimeLimitValueLab.attributedText = text
            
        
            if let iconUrl = URL(string: self.handleStatusImageV(JCWProductStatus(rawValue:model.status),model)){
//                 self.productStatusImageV.kf.setImage(with: ImageResource(downloadURL: iconUrl))
                self.productStatusImageV.kf_setImage(with: ImageResource(downloadURL: iconUrl))
            }
            self.repaymentTypeLabel.text = model.repaymentTypeLabel
            self.sminProfit.text = model.profitLabel
            self.updateButtonStatus(status: model.status)
           
            // 进度条 百分比
//            let strinvestPer = String(format: "%.0f", model.investPercent.cGFloat * 100) + "%"
//            self.processLab.text = strinvestPer
        }
    }
    
    //出借列表 使用的方法
    // MARK:卡顿方法不用理会 为了优化理财列表滑动 ，
    ///卡顿处理加载
    func kaDunHandler(_ modelTemp: Any?, _ listView: UITableView, _ indexPath: IndexPath){
        if let dataArr = modelTemp as? [ProjectModel] {
            let model = dataArr[indexPath.row]
            self.titleLab.text =  model.title
            self.tagView.tagsStr(model.tags)
//            self.productStatusImageV.image = self.handleStatusImageV(JCWProductStatus(rawValue:model.status),model)
            //标 的状态图片
            if let iconUrl = URL(string: self.handleStatusImageV(JCWProductStatus(rawValue:model.status),model)){
//                self.productStatusImageV.kf.setImage(with: ImageResource(downloadURL: iconUrl))
                self.productStatusImageV.kf_setImage(with: ImageResource(downloadURL: iconUrl))
            }else{
                self.productStatusImageV.image = UIImage();
            }
            
//            self.progressView.wl_setProgress(model.investPercent.cGFloat,JCWProductStatus(rawValue: model.status))
        }
    }
    
    /// 卡顿剩余部分处理
    func kaDunHandler2(_ modelTemp: Any?, _ listView: UITableView, _ indexPath: IndexPath) {
        if let dataArr = modelTemp as? [ProjectModel] {
            let model = dataArr[indexPath.row]
            
            self.titleLab.text =  model.title
            self.tagView.tagsStr(model.tags)
            //            self.productStatusImageV.image = self.handleStatusImageV(JCWProductStatus(rawValue:model.status),model)
            //标 的状态图片
            if let iconUrl = URL(string: self.handleStatusImageV(JCWProductStatus(rawValue:model.status),model)){
                //                self.productStatusImageV.kf.setImage(with: ImageResource(downloadURL: iconUrl))
                self.productStatusImageV.kf_setImage(with: ImageResource(downloadURL: iconUrl))
            }else{
                self.productStatusImageV.image = UIImage();
            }
            
            if model.cashRateShow == "1" {
                //model.cashRateProfit
                self.minProfitLab.attributedText = handlerProfit(model.profit,model.cashRateProfit)
            }else{
                //model.profitFloat
                self.minProfitLab.attributedText = handlerProfit(model.profit,model.profit)
            }
            // 期限 处理富文本编辑框
            let text = NSMutableAttributedString(string: model.plstimeLimitValue, attributes: [NSForegroundColorAttributeName:ColorFromRGBHex_WL(0x5B9CF8),NSFontAttributeName:Font_WL(20.aw_wl)])
            
            text.append(NSAttributedString(string: model.prdtimeLimitTypeLabel, attributes: [NSForegroundColorAttributeName : Normal_999Color,NSFontAttributeName:Font_WL(11.aw_wl)]))
            
            self.plstimeLimitValueLab.attributedText = text
            
            //---
            self.repaymentTypeLabel.text = model.repaymentTypeLabel
            self.sminProfit.text = model.profitLabel
            self.updateButtonStatus(status: model.status)
            
        }
    }
    func updateButtonStatus(status:String) {
        
        switch status {
        case "20": //预告中
            self.productButton.setTitle("预售中", for: .normal)
            self.productButton.setTitleColor(ColorFromRGBHex_WL(0xFDDB1A), for: .normal)
            self.productButton.layer.borderColor = ColorFromRGBHex_WL(0xFDDB1A).cgColor
            self.productButton.layer.borderWidth = 1
            self.productButton.layer.cornerRadius = 13;
            self.productButton.layer.masksToBounds = true
        case "30": //募集中
            self.productButton.setTitleColor(ColorFromRGBHex_WL(0x3A85EF), for: .normal)
            self.productButton.layer.borderColor = ColorFromRGBHex_WL(0x3A85EF).cgColor
            self.productButton.layer.borderWidth = 1
            self.productButton.layer.cornerRadius = 13;
            self.productButton.layer.masksToBounds = true
            self.productButton.setTitle("立即出借", for: .normal)
        case "40": //已告罄
            self.productButton.setTitleColor(ColorFromRGBHex_WL(0x9D9D9D), for: .normal)
            self.productButton.layer.borderColor = ColorFromRGBHex_WL(0x9D9D9D).cgColor
            self.productButton.layer.borderWidth = 1
            self.productButton.layer.cornerRadius = 13;
            self.productButton.layer.masksToBounds = true
            self.productButton.setTitle("已告罄", for: .normal)
        default:
             self.productButton.setTitle(" ", for: .normal)
             self.productButton.layer.borderColor = UIColor.white.cgColor
        }
        
    }
    
    /// 处理状态图片 剩余可投 募集总额
    private func handleStatusImageV(_ status: JCWProductStatus?, _ model: ProjectModel) -> String{
        self.surplusAmountLab.text = NSString(money: model.surplusAmount) as String
//        self.processLab.isHidden = false
        self.psurplusAmount.text = String(format: "%@: ", model.surplusAmountLabel)//"募集总额："
        if status == .saling {
            self.psurplusAmount.text = String(format: "%@: ", model.surplusAmountLabel)//"剩余可投："
            return ""
        }else if status == .prepareSale {
//            self.processLab.isHidden = true
            return model.statusIcon
        }else{
            self.surplusAmountLab.text = NSString(money: model.productsScale) as String
            return model.statusIcon
        }
        
    }
    
    ///处理利率 富文本
    private func handlerProfit(_ str1: String, _ str2: String) -> NSMutableAttributedString{
        let str11 = String(format: "%.2f", str1.cGFloat * 100)
        let temp = NSMutableAttributedString(string: str11,attributes: [NSFontAttributeName: Font_Auto(20)])
        temp.append(NSAttributedString(string: "%",attributes: [NSFontAttributeName: Font_Auto(13)]))
       
        if str2.isEmpty{
            return temp
       }else{
            let str22 = String(format: "%.2f",str2.cGFloat * 100)
            temp.append(NSAttributedString(string: "+\(str22)%", attributes: [NSFontAttributeName: Font_Auto(13)]))
            return temp
        }
    }
    
    ///快捷生成cell
    static func wl_createCellWithTableView(_ tableView: UITableView) -> ZYFinancialProductCell {
        
        let cellID =
            NSStringFromClass(ZYFinancialProductCell.self);
        if let cell = tableView.dequeueReusableCell(withIdentifier: cellID) as? ZYFinancialProductCell {
            return cell
        }else{
            return ZYFinancialProductCell(style: .default, reuseIdentifier: cellID)
        }
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.createContentView()
    }
    
    func createContentView() {
        self.selectionStyle = .none
        self.contentView.backgroundColor = UIColor.white
        self.contentView.addSubview(self.cv)
        
        //产品标题title 年化收益率minProfit 投资期限 plstimeLimitValue 剩余可投 surplusAmount
        self.titleLab.setSingleLineAutoResizeWithMaxWidth(JCWSCREENW)
        self.cv.addSubview(self.titleLab)
        _ = self.titleLab
            .sd_layout()
            .topSpaceToView(self.cv,17.5)?
            .leftSpaceToView(self.cv,15)?
            .heightIs(14)
        //活动标签
        self.cv.addSubview(self.tagView)
        _ = self.tagView
        .sd_layout()
        .centerYEqualToView(self.titleLab)?
        .leftSpaceToView(self.titleLab,5)
        
        //售罄 图标
        self.cv.addSubview(self.productStatusImageV)
        _ = self.productStatusImageV
            .sd_layout()
            .rightSpaceToView(self.cv,15)?
            .topSpaceToView(self.cv,0)?
            .widthIs(33)?
            .heightIs(23)
        //立即出借按钮
        self.cv.addSubview(self.productButton)
        if UIScreen.main.bounds.width == 320 {
            _ = self.productButton.sd_layout()?
                .rightEqualToView(self.productStatusImageV)?
                .topSpaceToView(self.cv,38)?.heightIs(25)?.widthIs(70)
        }else{
            _ = self.productButton.sd_layout()?
                .rightEqualToView(self.productStatusImageV)?
                .topSpaceToView(self.cv,38)?.heightIs(25)?.widthIs(90)
        }
       
        
        
         //年化收益 利率  10% + 1.5%
        self.minProfitLab.setSingleLineAutoResizeWithMaxWidth(120)
        self.cv.addSubview(self.minProfitLab)
        _ = self.minProfitLab
        .sd_layout()
        .topSpaceToView(self.titleLab,18)?
        .leftEqualToView(self.titleLab)?
        .heightIs(18)
        //分割线
        self.cv.addSubview(self.lineView)
        if UIScreen.main.bounds.width == 320 {
            _ = self.lineView.sd_layout()?
                .topSpaceToView(self.cv,60)?
                .leftSpaceToView(self.cv,115)?
                .heightIs(13)?
                .widthIs(1)
        }else{
            _ = self.lineView.sd_layout()?
                .topSpaceToView(self.cv,60)?
                .leftSpaceToView(self.cv,120+15)?
                .heightIs(13)?
                .widthIs(1)
        }
        
        
        //约定年化利率 文字
        self.cv.addSubview(self.sminProfit)
        self.sminProfit.setSingleLineAutoResizeWithMaxWidth(120)
        _ = self.sminProfit.sd_layout()?
            .leftEqualToView(self.titleLab)?
            .topSpaceToView(self.minProfitLab,10)?
            .heightIs(11)
        
//        self.sminProfit.setSingleLineAutoResizeWithMaxWidth(100)
//        self.cv.addSubview(self.sminProfit)
//        _ = self.sminProfit
//        .sd_layout()
//        .topSpaceToView(self.minProfitLab,25)?
//        .leftEqualToView(self.minProfitLab)?
//        .heightIs(11)
        
        // 投资期限 期限
        self.cv.addSubview(self.plstimeLimitValueLab)
        _ = self.plstimeLimitValueLab
            .sd_layout()?
            .leftSpaceToView(self.lineView,15)?
            .centerYEqualToView(self.minProfitLab)?
            .rightSpaceToView(self.productButton,10)?
            .heightIs(15)
        //每月付息，到期还本
        self.cv.addSubview(self.repaymentTypeLabel)
        _ = self.repaymentTypeLabel.sd_layout()?
            .leftEqualToView(self.plstimeLimitValueLab)?
            .centerYEqualToView(self.sminProfit)?
            .heightIs(11)
        
     
       //进度条
//        self.cv.addSubview(self.progressView)
//           _ = self.progressView
//            .sd_layout()
//            .rightEqualToView(self.plstimeLimitValueLab)?
//            .topSpaceToView(self.minProfitLab,10)?
//            .leftEqualToView(self.titleLab)?
//            .heightIs(42.5)
        
        
        
//        // 投资期限 值：30
//        self.pplstimeLimitValue.setSingleLineAutoResizeWithMaxWidth(100)
//        self.cv.addSubview(self.pplstimeLimitValue)
//        _ = self.pplstimeLimitValue
//        .sd_layout()
//        .leftSpaceToView(self.cv,JCWSCREENW/2-30)?
//        .centerYEqualToView(self.minProfitLab)?
//        .heightIs(13)
        
       // self.cv.addSubview(self.progressView)
        
      
        
        // self.plstimeLimitValueLab.backgroundColor = ColorFromRGBHex_WL(0x5b9cf8)
        
        
//        // 剩余可投
//        self.psurplusAmount.setSingleLineAutoResizeWithMaxWidth(100)
//        self.cv.addSubview(self.psurplusAmount)
//        _ = self.psurplusAmount
//        .sd_layout()
//        .leftEqualToView(self.titleLab)?
//        .bottomSpaceToView(self.cv,6)?
//        .heightIs(14)
        
//        // 剩余可投 金额
//        self.surplusAmountLab.setSingleLineAutoResizeWithMaxWidth(100)
//        self.cv.addSubview(self.surplusAmountLab)
//        _ = self.surplusAmountLab
//        .sd_layout()
//        .leftSpaceToView(self.psurplusAmount,0)?
//        .bottomSpaceToView(self.cv,6)?
//        .heightIs(14)
        
        
//        // 进度条 百分比
//        self.cv.addSubview(self.processLab)
//        _ = self.processLab
//        .sd_layout()
//        .rightSpaceToView(self.cv,15)?
//        .leftSpaceToView(self.surplusAmountLab,0)?
//        .centerYEqualToView(self.surplusAmountLab)?
//        .heightIs(14)
       
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    ///contentV
    let cv: UIView = {
        let temp = UIView(frame: CGRect(x: 8, y: 0, width: JCWSCREENW-16, height: 100))
        temp.backgroundColor =  UIColor.white
        temp.layer.cornerRadius = 5
        
        //阴影效果卡顿 用图最好
        temp.layer.shadowOffset = CGSize(width: 0, height: 0)
        temp.layer.shadowColor = UIColor.gray.cgColor
        temp.layer.shadowOpacity = 0.4
        
        return temp
    }()
    
    ///产品标题title
    let titleLab: UILabel = {
       let temp = UILabel()
        temp.textColor = Normal_333Color
        temp.font = BoldFont_WL(14)
        temp.text = " "
        return temp
    }()
    
    
    ///标签 eg: 双11 加息券活动
    let tagView: JCWActiveTagView = {
        let temp = JCWActiveTagView(UIColor.white, true, nil)
        return temp
    }()
    
    ///年化收益 利率 minProfit
    let minProfitLab: UILabel = {
        let temp = UILabel()
        temp.textColor = ColorFromRGBHex_WL(0x5b9cf8)
        temp.font = BoldFont_WL(24)
        temp.text = " "
        return temp
    }()
    //约定年化利率 lable文案
    let sminProfit: UILabel = {
        let temp = UILabel()
        temp.textColor = JCWColor_minProfit_gray
        temp.font = Font_WL(11)
         temp.text = "约定年化利率"
        return temp
    }()
    
//    ///投资期限plstimeLimitValue
//    let pplstimeLimitValue: UILabel = {
//       let temp = UILabel()
//        temp.textAlignment = .center
//        temp.textColor = JCWColor_minProfit_gray
//        temp.font = Font_Auto(13)
//        temp.text = "期限 "
//        return temp
//    }()
    
    /// 投资期限 plstimeLimitValue
    let plstimeLimitValueLab: UILabel = {
        let temp = UILabel()
        temp.textColor = Normal_333Color
        temp.font = Font_Auto(13)
        temp.text = " "
        temp.textAlignment = .left
        return temp
    }()
    
    
    ///剩余可投
    let psurplusAmount: UILabel = {
        let temp = UILabel()
        temp.textAlignment = .center
        temp.textColor = Normal_999Color
        temp.font = Font_Auto(13)
//        temp.text = "剩余可投："
        return temp
    }()

    ///剩余可投 金额
    let surplusAmountLab: UILabel = {
        let temp = UILabel()
        temp.textColor = Normal_999Color
        temp.font = Font_Auto(13)
        temp.text = " "
        return temp
    }()
    
//    ///进度条
//    let progressView: ZMBLineProgressView = {
//        let temp = ZMBLineProgressView()
//        return temp
//    }()
    
    /// 状态图eg: 预售中 还款中
    let productStatusImageV: UIImageView = {
        let temp =  UIImageView()
        return temp
    }()
    
//    ///进度条百分比
//    let processLab: UILabel = {
//        let temp = UILabel()
//        temp.textColor = Normal_999Color
//        temp.font = Font_Auto(13)
//        temp.text = " "
//        temp.textAlignment = .right
//        return temp
//    }()
    //Mark: 还款方式: 每月付息 ，f到期还本
    let repaymentTypeLabel:UILabel = {
        let temp = UILabel()
        temp.textColor = Normal_999Color
        temp.font = Font_Auto(11)
        temp.text = "每月付息 ，到期还本"
        return temp
    }()
    
    ///按钮 状态：  立即出借
    let productButton:UIButton = {
       let tempButton = UIButton(type: .custom)
        tempButton.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        tempButton.isUserInteractionEnabled = false
       return tempButton
    }()
    ///分割线
    let lineView:UIView = {
       let line = UIView()
       line.backgroundColor = ColorFromRGBHex_WL(0xDCDCDC)
        return line
    }()
    
    
}
